package com.xiaolin.dao.impl;

import com.xiaolin.bean.Cart;
import com.xiaolin.dao.BaseDAO;
import com.xiaolin.dao.CartDAO;

import java.sql.Connection;
import java.util.List;

public class CartDaoImpl extends BaseDAO<Cart> implements CartDAO {
    /**
     * 获取购物车全部商品
     *
     * @param conn
     * @return
     */
    @Override
    public List<Cart> getAll(Connection conn,String user) {
        String sql ="SELECT * FROM shoppCart WHERE user ="+user;
        List<Cart> list = getForList(conn,sql);
        return list;
    }

    /**
     * @param conn
     * @param cart
     */
    @Override
    public void insert(Connection conn, Cart cart) {
        String sql = "INSERT INTO shoppcart(id,name,price,type,num,user) VALUES(?,?,?,?,?,?)";
        update(conn,sql,cart.getId(),cart.getName(),cart.getPrice(),cart.getType(),cart.getNum(),cart.getUser());
    }

    /**
     * 通过id删除购物车里面的东西
     *
     * @param conn
     * @param id
     */
    @Override
    public void deleteById(Connection conn, int id,String user) {
        String sql = "DELETE FROM shoppCart WHERE id = ? AND user = ?";
        update(conn,sql,id,user);
    }
}
